package com.biostime.rpc.terminal.service.finagle;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.biostime.rpc.terminal.ResponseCode;
import com.biostime.rpc.terminal.service.TerminalVoService;
import com.biostime.rpc.terminal.thrift.TerminalService;
import com.biostime.rpc.terminal.thrift.inout.FTerminal;
import com.biostime.rpc.terminal.thrift.inout.Location;
import com.biostime.rpc.terminal.thrift.inout.common.BaseResponse;
import com.biostime.rpc.terminal.thrift.inout.request.TSFindNearTerminalsRequest;
import com.biostime.rpc.terminal.thrift.inout.response.FTerminalListResponse;
import com.biostime.rpc.terminal.util.LogUtils;
import com.biostime.rpc.terminal.util.MapperUtils;
import com.biostime.rpc.terminal.util.RespFactory;
import com.biostime.rpc.terminal.util.ResponseCodeUtil;
import com.mama100.common.finagle.util.FinagleRPC;
import com.twitter.util.ExecutorServiceFuturePool;
import com.twitter.util.Function0;
import com.twitter.util.Future;

/**
 * 类功能描述: 终端门店服务
 *
 * @version 1.0
 * @author zhuhaitao
 * @createDate 2015年12月21日 下午3:11:24
 */
@FinagleRPC
@Service
public class TerminalServiceImpl implements TerminalService.ServiceIface {

	private Logger log = LoggerFactory.getLogger(TerminalServiceImpl.class);

	@Autowired
	private ExecutorServiceFuturePool futurePool;
	@Autowired
	private TerminalVoService terminalVoService;
	@Autowired
	private MapperUtils mapper;

	/**
	 * 查询附近门店
	 */
	@Override
	public Future<FTerminalListResponse> findNearTerminals(TSFindNearTerminalsRequest request) {
		return futurePool.apply(new Function0<FTerminalListResponse>() {
			@Override
			public FTerminalListResponse apply() {
				LogUtils.info(log, "findNearTerminals：", request);
				FTerminalListResponse response = new FTerminalListResponse();
				BaseResponse baseResp = RespFactory.get(request.getBaseReq());
				response.setBaseResp(baseResp);
				try {
					long t1 = System.currentTimeMillis();
					Location location = request.getLocation();
					Long distance = request.getDistance() != 0 ? request.getDistance() : 10000;
					String terminalCode = request.getTerminalCode();
					String terminalName = request.getTerminalName();
					int isStaff = request.getIsStaff();
					List<FTerminal> list = terminalVoService.findNearTerminals(location, distance, terminalCode, terminalName, isStaff);
					response.setValue(list);
					baseResp.setRespCode(ResponseCode.SUCCESS.getCode());
					baseResp.setRespDesc(ResponseCode.SUCCESS.getDesc());
					long t2 = System.currentTimeMillis();
					// LogUtils.info(log, "findNearTerminals：", response);
					LogUtils.info(log, "findNearTerminals查询数据库耗时：", t2 - t1 + "ms");
					return response;
				} catch (Exception e) {
					baseResp.setRespCode("500");
					baseResp.setRespDesc("查询门店异常，请联系相关负责人");
					LogUtils.error(log, e, "findNearTerminals", response);
					return response;
				}
			}
		});
	}
}
